package com.zch.single;

/**
 * 单例模式-懒汉式
 * 使用对象时才创建对象
 * 存在线程安全问题
 */
public class SingleTon02 {
    public static void main(String[] args) {
        Cat instance = Cat.getInstance();
        Cat instance2 = Cat.getInstance();
        System.out.println(instance);
        System.out.println(instance2);
    }
}

class Cat {
    private String name;
    private static Cat cat;

    private Cat(String name) {
        System.out.println("构造器调用..");
        this.name = name;
    }

    public static Cat getInstance() {
        if (cat == null) {
            cat = new Cat("小可爱");
        }
        return cat;
    }
}
